package com.code.springbootquartz.config;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;

/*
 * @author: liyufei
 * @date: 2018-07-13 18:06
 */
@Configuration
public class QuartzConfig {

    @Autowired
    private SpringJobFactory springJobFactory;

    @Bean(name="SchedulerFactory")
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setAutoStartup(true);   // 自动开启服务
        //factory.setStartupDelay(5);//延时5秒启动
        //factory.setQuartzProperties(quartzProperties());  // 将quartz数据保存至mysql
        factory.setJobFactory(springJobFactory);
        return factory;
    }

    @Bean(name="Scheduler")
    public Scheduler scheduler() throws IOException {
        return schedulerFactoryBean().getScheduler();
    }

    /*@Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }*/

    /*
     * quartz初始化监听器
     */
    /*@Bean
    public QuartzInitializerListener executorListener() {
        return new QuartzInitializerListener();
    }*/
}
